import { _decorator, Component, Node } from 'cc';
import { MainWork } from './MainWork';
import Util from './Util';
import *as cc from "cc";
import DatasSpace from './Tool/DatasSpace';
import { PREVIEW } from 'cc/env';
import EventMgr from './Tool/EventMgr';
import { EVENT_TYPE, Levels } from '../Cube/Levels';
import { AudioMgr, ENUM_CLIP } from './Tool/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('MainGame')
export class MainGameUI extends MainWork {

  @property([cc.Label])
  propNumLabel: cc.Label[] = [];

  @property(cc.Label)
  countdownTimeLabel: cc.Label = null;

  @property(cc.Label)
  curretLevelLabel: cc.Label = null;

  @property([cc.Node])
  propShipinNode: cc.Node[] = [];


  static inst: MainGameUI = null;
  public timer: number = 10;
  public timerCall = null;
  isEndTimer: boolean = false;

  BtnCODE(event, customEventData) {
    // console.log( ' 调用');
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    var btnName = event.target.name;
    switch (btnName) {
      case "btnMainGamePause":
        this.mainGamePause();
        break;
    }

  }

  //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  mainGamePause() {
    Util.Instance.GloberLoadPrefab('Pause', 2, (res) => {
      let newNode = cc.instantiate(res);
      this.node.addChild(newNode);
    }, () => {

    });
  }


  start() {
    MainGameUI.inst = this;
    EventMgr.on(EVENT_TYPE.EVENT_UPD_GAMEINFO, this.updGameInfo, this);
    EventMgr.on(EVENT_TYPE.EVENT_UPD_PROP_HEPAI, this.updPropItem, this);
    // this.initMainGameProp();
    // this.countdownTimeLabel.string = Util.Instance.formatSeconds(this.timer, 'i:s');
    // this.startCountDown();
    // this.curretLevelLabel.string = '第' + (DatasSpace.Instance.level + 1) + '关';
    // console.log("当前关卡是: ", DatasSpace.Instance.level + 1);
    this.updGameInfo();
  }

  protected onDestroy(): void {
    EventMgr.off(EVENT_TYPE.EVENT_UPD_GAMEINFO, this.updGameInfo, this);
    EventMgr.off(EVENT_TYPE.EVENT_UPD_PROP_HEPAI, this.updPropItem, this);

    AudioMgr.Instance.stop();
  }

  initMainGameProp() {
    for (let i = 0; i < this.propNumLabel.length; i++) {
      if (DatasSpace.Instance.propNumArr[i] <= 0) {
        this.propNumLabel[i].node.active = false;
        this.propShipinNode[i].parent.getChildByName('shareTag').active = false;
        this.propShipinNode[i].parent.getChildByName('add').active = true;
        if (i == 2 && DatasSpace.Instance.ShareCount > 0) {
          this.propShipinNode[i].parent.getChildByName('shareTag').active = false;
          this.propShipinNode[i].active = false;
        } else {
          this.propShipinNode[i].parent.getChildByName('shareTag').active = false;
          this.propShipinNode[i].active = true;
        }
      } else {
        this.propShipinNode[i].parent.getChildByName('add').active = false;
        this.propShipinNode[i].parent.getChildByName('shareTag').active = false;
        this.propShipinNode[i].active = false;
        this.propNumLabel[i].string = DatasSpace.Instance.propNumArr[i] + '';
        this.propNumLabel[i].node.active = true;
      }

    }
  }

  update(deltaTime: number) {

  }

  /**
   * 开始倒计时
   * time 2024 5 29 2:41
   */
  startCountDown() {
    if(DatasSpace.Instance.level==0){
      return;
    }
    this.isEndTimer = false;
    this.timerCall = () => {
      if (DatasSpace.Instance.gamePause) return;
      if (this.timer < 1) {
        //结束游戏
        if (this.isEndTimer == false) {
          this.unschedule(this.timerCall);
          this.isEndTimer = true;
          cc.game.emit('endGame');
          return;
        }

      }
      this.timer--;
      this.countdownTimeLabel.string = Util.Instance.formatSeconds(this.timer, 'i:s');
    }

    this.schedule(this.timerCall, 1);
  }

  updGameInfo() {
    this.unscheduleAllCallbacks();
    this.timer = Levels[DatasSpace.Instance.level] ? Levels[DatasSpace.Instance.level].Time : 900;
    this.initMainGameProp();
    this.countdownTimeLabel.string = Util.Instance.formatSeconds(this.timer, 'i:s');
    this.startCountDown();
    this.curretLevelLabel.string = '第' + (DatasSpace.Instance.level + 1) + '关';
    console.log("当前关卡是: ", DatasSpace.Instance.level + 1);
  }

  updPropItem(isShow) {
    this.propShipinNode[2].parent.getComponent(cc.Button).interactable = isShow;
    this.propShipinNode[2].parent.getComponent(cc.Sprite).grayscale = !isShow;
  }
}


